<?php
	class ExerciceManager{
		
		private $_db;
		
		public function __construct($db){
				$this->setDb($db);	
		}
		
		public function setDb(PDO $db){
				$this->_db = $db;
		}		
		
		public function add(Exercice $exercice){// permet d'ajouter un exercice
				$q = $this->_db->prepare('INSERT INTO exercice SET intitule = :intitule, duree_seconde = :duree_seconde, type = :type, idRefExemple = :idRefExemple');
				$q->bindValue(':intitule', $exercice->intitule());
				$q->bindValue(':duree_seconde', $exercice->duree_seconde());
				$q->bindValue(':type', $exercice->type());
				$q->bindValue(':idRefExemple', $exercice->idRefExemple());
				$q->execute();			
		}
		
		public function delete(Exercice $exercice){// permet de supprimer un exercice
				$this->_db->exec('DELETE FROM exercice WHERE id = '.$exercice->id());
		}
		
		public function update(Exercice $exercice){// permet de modifier un exercice
				$q = $this->_db->prepare('UPDATE exercice SET intitule = :intitule, duree_seconde = :duree_seconde, type = :type, idRefExemple = :idRefExemple WHERE id ='.$exercice->id());
				$q->bindValue(':intitule', $exercice->intitule());
				$q->bindValue(':duree_seconde', $exercice->duree_seconde());
				$q->bindValue(':type', $exercice->type());
				$q->bindValue(':idRefExemple', $exercice->idRefExemple());
				$q->execute();			
		}
		
		public function get($id){// retourne l'objet exercice d'id correspondant
				$id = (int) $id;
				$q = $this->_db->query('SELECT * FROM exercice WHERE id = '.$id);
				$donnees = $q->fetch();
				return new Exercice($donnees);			
		}
		
		  public function getList(){
				$exercice = array();
				$q = $this->_db->query('SELECT * FROM exercice WHERE idRefExemple = 0 ORDER BY id');
				while ($donnees = $q->fetch()){
					$exercice[] = new Exercice($donnees);
				}
				return $exercice;
		  }		
		
		public function getByTest($idTest){// retourne un tableau des id des exercices correspondant à un test.
				$exercice = array();
				$q = $this->_db->query('SELECT * FROM test_exercice WHERE idRefTest = '.$idTest);
				while ($donnees = $q->fetch()){
					$exercice[] = $donnees;
				}
				return $exercice;				
		}
		
		public function getByTestOrder($idTest){// retourne un tableau des id des exercices correspondant à un test dans l'ordre.
				$exercice = array();
				$q = $this->_db->query('SELECT * FROM test_exercice WHERE idRefTest = '.$idTest.' ORDER BY ordre');
				while ($donnees = $q->fetch()){
					$exercice[] = $donnees;
				}
				return $exercice;				
		}				
		  
		  public function getNbQuestion($idExo){ //  retourne le nombre de question d'un exercice
				$q = $this->_db->query('SELECT COUNT(*) FROM question WHERE idRefExercice = '.$idExo);
				$donnees = $q->fetch();
				return $donnees[0];				  
		  }	

		  public function getNbReponse($idExo){ //  retourne le nombre de réponse d'un exercice
				$q = $this->_db->query('SELECT COUNT(*) FROM reponse, exercice, question WHERE reponse.idRefQuestion = question.id AND exercice.id = '.$idExo.' AND question.idRefExercice = exercice.id');
				$donnees = $q->fetch();
				return $donnees[0];				  
		  }	
		  
		  public function getNbReponseJuste($idExo){ //  retourne le nombre de réponse juste d'un exercice
				if($this->get($idExo)->type() != "TT")
				{
					$q = $this->_db->query('SELECT COUNT(*) FROM reponse, exercice, question WHERE reponse.idRefQuestion = question.id AND exercice.id = '.$idExo.' AND question.idRefExercice = exercice.id AND reponse.correcte = 1');
					$donnees = $q->fetch();
					return $donnees[0];
				} else {
					$q = $this->_db->query('SELECT COUNT(*) FROM txt_trous, question, exercice WHERE txt_trous.idQuestion = question.id AND question.idRefExercice = exercice.id AND exercice.id = '.$idExo);
					$donnees = $q->fetch();
					return $donnees[0];
				}
		  }
		  
		  public function getNbByTest($id){ // retourne le nombre d'exercice d'un test en fonction de son id
				$q = $this->_db->query('SELECT COUNT(*) FROM test_exercice WHERE idRefTest = '.$id);
				$donnees = $q->fetch();
				return $donnees[0];				  
		  }
		  
		  public function getExemple($id){ // récupère l'exemple d'un exercice
				$id = (int) $id;
				$q = $this->_db->query('SELECT * FROM exercice WHERE idRefExemple = '.$id);
				$donnees = $q->fetch();
				if($donnees != false){
					return new Exercice($donnees);
				} else {
					return false;
				}
		  }
	}
?>